<?php

/**
 * BlogController is the default controller for this application
 */
class BlogController extends CustomControllerBase {
	
	/**
	 * UserBlogCommentDataService object
	 *
	 * @var UserBlogCommentDataService object
	 */
	public $userBlogCommentDataService = null;
	
	/**
	 * 
	 * System Category Data Service Object
	 * @var SystemCategoryDataService object
	 */
	public $systemCategoryDataService = null;
	
	/**
	 * Constructor of index
	 */
	public function __construct() {
		parent::__construct ();
		
		// Set page name
		$this->setPageName ( 'user_blog' );
		$this->setModuleName ( "Blog" );
		
		//Check if Blogging Module is enabled
		if ($this->currentPackageOption) {
			$module = MODULE_BLOGGING;
			$isEnabled = $this->currentPackageOption->isEnabled ( $module );
			if ($isEnabled == false) {
				//$url = Zend_Registry::get('config')->APPLICATION_URL .'/'.$this->ownerName;
				$url = $this->getApplicationUrl ();
				CommonUtils::redirect ( $url );
				return - 1;
			}
		}
		
		// Initialise userBlogCommentDataService
		$this->userBlogCommentDataService = new UserBlogCommentDataService ();
		$this->systemCategoryDataService = new SystemCategoryDataService ();
		// Get list category
		$arrElement = array ();
		$arrElement ['ownerId'] = $this->ownerId;
		$arrElement ['type'] = CATEGORY_BLOG;
		$catList = $this->systemCategoryDataService->getItems ( $arrElement );
		$this->smarty->assign ( 'catList', $catList );
		$this->smarty->assign ( 'catListCount', count ( $catList ) );
	}
	
	/**
	 * The "index" action is the default action for all controllers -- the
	 * landing page of the site.
	 *
	 * @return void
	 *
	 */
	public function indexAction() {
		// Get 5 latest Blog entry
		$arrElement = array ();
		$arrElement ['status'] = UserBlogEntity::ACTIVE;
		$arrElement ['ownerId'] = $this->ownerId;
		$cat = $this->getQuery ( 'cat' );
		if ($cat != "") {
			$arrElement ['categoryId'] = $cat;
		}
		$numRecordPerPage = ITEM_PER_PAGE;
		// Pager
		$start = $this->getQuery ( 'start', 0 );
		if ($start == '' || $start < 0) {
			$start = 0;
		}
		
		// Get total records of product options based on search criterias
		$totals = $this->userBlogDataService->getTotals ( $arrElement, " id DESC" );
		
		$itemsList = $this->userBlogDataService->getItems ( $arrElement, " id DESC", $start, $numRecordPerPage );
		
		if ($start > $totals) {
			$start = 0;
		}
		//$link = Zend_Registry::get('config')->APPLICATION_URL .'/'. $this->ownerName ."/Blog/Index";
		$link = $this->getApplicationUrl () . "Blog/Index";
		$link .= $cat != '' ? "/cat/{$cat}" : '';
		$pageLink = '';
		if ($totals > $numRecordPerPage) {
			$pageUtils = new PagerUtils ( $start, $totals, $numRecordPerPage, 10, $link );
			$pageLink = $pageUtils->writePagesLinks ( 2, 'start' );
			$pageLink .= "<br><span class=\"paing_text_nolink\">" . $pageUtils->writePagesCounter () . "</span>";
		}
		
		//Refresh the Description data content
		if (count ( $itemsList ) > 0) {
			foreach ( $itemsList as $item ) {
				$item->body = htmlspecialchars_decode ( $item->body, ENT_QUOTES );
			}
		}
		
		$this->smarty->assign ( 'blogItemList', $itemsList );
		$this->smarty->assign ( 'pageLink', $pageLink );
		$this->smarty->assign ( 'numberBlogItemList', count ( $itemsList ) );
		
		if ($this->isMobileMode == false) {
			// Assign Smarty Content
			$this->assignSmartyContent ( 'Blog/index.tpl' );
		} else {
			$this->assignSmartyMobileContent ( 'Blog/m_index.tpl' );
		}
	
	}
	
	/**
	 *
	 * Blog Details Action
	 */
	public function detailsAction() {
		$this->setPageName ( 'user_blog_details' );
		
		$id = $this->getQuery ( 'id' );
		if ($id == '') {
			$id = $this->getPost ( 'id', '' );
		}
		
		$id = intval ( $id );
		//Increase Views
		$this->userBlogDataService->updateView ( $id );
		
		// Get data
		$userBlogEntity = $this->userBlogDataService->getById ( $id );
		if (empty ( $userBlogEntity )) {
			// Go on to login page
			$url = $this->getApplicationUrl ();
			CommonUtils::redirect ( $url );
			return - 1;
		}
		
		$this->smarty->assign ( 'blogEntity', $userBlogEntity );
		$this->setMetaDescription ( $userBlogEntity->metaDescription );
		$this->setPageTitle ( $userBlogEntity->metaTitle );
		$this->setMetaKeywords ( $userBlogEntity->metaKeywords );
		
		$actionForm = $this->getPost ( 'actionForm', '' );
		if (! empty ( $actionForm ) && $actionForm == 'submit') {
			//Check login user
			if ($this->authentication->getCurrentUserId () == null) {
				
				if ($this->ownerId != "") {
					$url = $this->getApplicationUrl () . "Login";
					CommonUtils::redirect ( $url );
					return - 1;
				}
			} else {
				$usersDataService = new UsersDataService ();
				$currentUser = $usersDataService->getById ( $this->authentication->getCurrentUserId () );
				if (! empty ( $currentUser ) && $currentUser->userType != ROLE_MEMBER_USER) {
					$url = $this->getApplicationUrl () . "Login";
					CommonUtils::redirect ( $url );
					return - 1;
				}
			}
			
			$comment = $this->getPost ( 'comment', '' );
			$userBlogCommentEntity = $this->userBlogCommentDataService->beforeCreate ();
			$userBlogCommentEntity->customerId = $this->authentication->getCurrentUserId ();
			$userBlogCommentEntity->blogId = $id;
			$userBlogCommentEntity->content = $comment;
			$userBlogCommentEntity->commentId = 0;
			$userBlogCommentEntity->status = UserBlogCommentEntity::ACTIVE;
			$this->userBlogCommentDataService->create ( $userBlogCommentEntity );
			
			//Update CommentCount for Blog Entry
			$userBlogEntity->commentCount = $userBlogEntity->commentCount + 1;
			$this->userBlogDataService->update ( $userBlogEntity );
			$message = 'Your request has just sent successfully';
		}
		
		// Get All Comments of that Blog Entry
		$listComments = $this->userBlogCommentDataService->getAllQuestions ( $id );
		
		// Get Next and Previous Blog Entry
		//$nextEntry = $this->userBlogDataService->getNextEntry($id);
		//$prevEntry = $this->userBlogDataService->getPreviousEntry($id);
		

		$this->smarty->assign ( 'listComments', $listComments );
		$this->smarty->assign ( 'id', $id );
		$this->smarty->assign ( 'userBlogCommentDataService', $this->userBlogCommentDataService );
		
		if ($this->isMobileMode == false) {
			//Assign Smarty Content
			$this->assignSmartyContent ( 'Blog/details.tpl' );
		} else {
			$this->assignSmartyMobileContent ( 'Blog/m_details.tpl' );
		}
	}
}
